package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.SCompany;
import com.example.service.SCompanyService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/company")
@Api(tags = "企宣相关接口",value = "企宣相关接口")
public class SCompanyController {
    @Autowired
    SCompanyService sCompanyService;
//    @GetMapping
//    @RequiresPermissions("15")
//    public Result<List<SCompany>> getCompany(){
//        List<SCompany> allCompany = sCompanyService.getAllCompany();
//        return Result.success(allCompany);
//    }
    @PostMapping
    public Result<?> save(@RequestBody SCompany sCompany) {
        sCompanyService.save(sCompany);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody SCompany sCompany) {
        sCompanyService.updateById(sCompany);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        sCompanyService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> findById(@PathVariable Long id) {
        return Result.success(sCompanyService.getById(id));
    }
    @GetMapping("/page")
    @RequiresPermissions("15")
    public Result<?> findPage(@RequestParam(required = false, defaultValue = "") String name,
                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<SCompany> query = Wrappers.<SCompany>lambdaQuery().like(SCompany::getLinkName, name).orderByDesc(SCompany::getId);
//        if (StrUtil.isNotBlank(name)) {
//            query.like(SCompany::getUrl, name);
//        }
        IPage<SCompany> page = sCompanyService.page(new Page<>(pageNum, pageSize), query);
        return Result.success(page);
    }

    @DeleteMapping("/delBatch")
    public Result<?> deleteBatch(@RequestBody List<String> ids) {

        sCompanyService.removeByIds(ids);
        return Result.success();
    }
}
